using System;
using System.Collections.Generic;
using System.Xml;
using System.Collections.ObjectModel;

namespace planning
{
	public class Planning : ObservableObject
	{
		// ----- ATTRIBUTS -----
		private NomAnnee annee;
		private NomDivision division;
		private string promotion;
		private ICollection<Ressource> ressources = new ObservableCollection<Ressource>();
		private ICollection<Semaine> semaines = new ObservableCollection<Semaine>();

		// ----- CONSTRUCTEURS -----
		public Planning()
		{
			// Start of user code for default constructor
			// End of user code for default constructor
		}

		// ----- ACCESSEURS -----
		public NomAnnee Annee
		{
			get { return annee; }
			set { annee = value; ObjectChanged("annee"); }
		}

		public NomDivision Division
		{
			get { return division; }
			set { division = value; ObjectChanged("division"); }
		}

		public string Promotion
		{
			get { return promotion; }
			set { promotion = value; ObjectChanged("promotion"); }
		}

		public ICollection<Ressource> Ressources
		{
			get { return ressources; }
			set { ressources = value; ObjectChanged("ressources"); }
		}

		public ICollection<Semaine> Semaines
		{
			get { return semaines; }
			set { semaines = value; ObjectChanged("semaines"); }
		}


		private static Dictionary<string, Planning> references = new Dictionary<string, Planning>();

		public static Planning fromXml(XmlNode node)
		{
			Planning planning;
			XmlNode refAttribute = node.SelectSingleNode("@ref");
			XmlNode idAttribute = node.SelectSingleNode("@id");

			if(refAttribute != null) {
				if(references.ContainsKey(refAttribute.Value)) {
					return references[refAttribute.Value];
				} else {
					planning = new Planning();
					references.Add(refAttribute.Value, planning);
					return planning;
				}
			}

			if(idAttribute != null && references.ContainsKey(idAttribute.Value)) {
				planning = references[idAttribute.Value];
			} else {
				planning = new Planning();

				if(idAttribute != null) {
					references.Add(idAttribute.Value, planning);
				}
			}

			if(node.SelectSingleNode("@annee") != null)
				planning.Annee = (NomAnnee) Enum.Parse(typeof(NomAnnee), node.SelectSingleNode("@annee").Value);

			if(node.SelectSingleNode("@division") != null)
				planning.Division = (NomDivision) Enum.Parse(typeof(NomDivision), node.SelectSingleNode("@division").Value);

			if(node.SelectSingleNode("@promotion") != null)
				planning.Promotion = node.SelectSingleNode("@promotion").Value;

			foreach (XmlNode subElement in node.SelectNodes("ressources/*"))
			{
				Ressource subObject = Ressource.fromXml(subElement);
				planning.Ressources.Add(subObject);
			}

			foreach (XmlNode subElement in node.SelectNodes("semaines/*"))
			{
				Semaine subObject = Semaine.fromXml(subElement);
				planning.Semaines.Add(subObject);
			}

			

			return planning;

		}

		public virtual XmlElement toXml(XmlDocument document)
		{
			// Start of user code for toXml
            XmlElement element = document.CreateElement("Planning");
            XmlAttribute anneeAttribute = document.CreateAttribute("annee");
            anneeAttribute.Value = Annee.ToString();
            element.Attributes.Append(anneeAttribute);

            XmlAttribute divisionAttribute = document.CreateAttribute("division");
            divisionAttribute.Value = Division.ToString();
            element.Attributes.Append(divisionAttribute);

            XmlAttribute promotionAttribute = document.CreateAttribute("promotion");
            promotionAttribute.Value = Promotion;
            element.Attributes.Append(promotionAttribute);

            XmlElement ressourcesElement = document.CreateElement("ressources");
            element.AppendChild(ressourcesElement);
            foreach (Ressource subObject in Ressources) {
                ressourcesElement.AppendChild(subObject.toXml(document));
            }

            XmlElement semainesElement = document.CreateElement("semaines");
            element.AppendChild(semainesElement);
            foreach (Semaine subObject in Semaines) {
                semainesElement.AppendChild(subObject.toXml(document));
            }

            return element;
			// End of user code for toXml
		}


	}

}
